﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

//@author A0083726Y
namespace EZXPNS
{
    public partial class SetGoalSavingForm : Form
    {
        MainForm mainform;
        public SetGoalSavingForm(MainForm frm)
        {
            InitializeComponent();
            mainform = frm;
        }

        private void SetGoalSavingForm_Load(object sender, EventArgs e)
        {
            StreamReader inputfile;
            inputfile = File.OpenText("../../GoalSaving.txt");
            CurrentGoal.Text = "$ " + inputfile.ReadLine();
            inputfile.Close();
        }

        private void Set_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("Nothing is entered!");
                return;
            }

            decimal temp;
            if (!decimal.TryParse(textBox1.Text, out temp))
            {
                MessageBox.Show("Input should be a number!");
                return;
            }
            else
            {
                StreamWriter outputfile;
                outputfile = File.CreateText("../../GoalSaving.txt");
                outputfile.WriteLine(temp.ToString());
                outputfile.Close();
            }

            MessageBox.Show("New goal saving is set successfully!");
            this.Close();

        }
        
        private void SetGoalSavingForm_Closing(object sender, FormClosingEventArgs e)
        {
            //Update alert information and goal saving display on main form
            mainform.AlertLabel.Text = EzXpns.Alert();

            decimal totalIncome = 0m;
            for (int i = 0; i < EzXpns._incomeList.Count; i++)
            {
                totalIncome += EzXpns._incomeList[i].Amount;
            }

            decimal totalExpense = 0m;
            for (int i = 0; i < EzXpns._expenseList.Count; i++)
            {
                totalExpense += EzXpns._expenseList[i].Amount;
            }


            StreamReader inputfile4;
            inputfile4 = File.OpenText("../../GoalSaving.txt");
            decimal goal = decimal.Parse(inputfile4.ReadLine());
            mainform.goalSaving.Text = "S$" + goal.ToString();
            inputfile4.Close();

            if (totalExpense > totalIncome)
                mainform.AlertLabel.ForeColor = Color.Red;
            else if ((totalIncome - totalExpense) < goal * 0.2m)
                mainform.AlertLabel.ForeColor = Color.Orange;
            else if ((totalIncome - totalExpense) < goal * 0.5m)
                mainform.AlertLabel.ForeColor = Color.YellowGreen;
            else if ((totalIncome - totalExpense) < goal * 0.8m)
                mainform.AlertLabel.ForeColor = Color.Green;
            else if ((totalIncome - totalExpense) < goal)
                mainform.AlertLabel.ForeColor = Color.Blue;
            else
                mainform.AlertLabel.ForeColor = Color.Purple;

            //progressbar
            if (totalExpense > totalIncome)
            {
                mainform.tenpercent.BackColor = Color.Red;
                mainform.twentypercent.BackColor = Color.Red;
                mainform.thirtypercent.BackColor = Color.Red;
                mainform.fortypercent.BackColor = Color.Red;
                mainform.fiftypercent.BackColor = Color.Red;
                mainform.sixtypercent.BackColor = Color.Red;
                mainform.seventypercent.BackColor = Color.Red;
                mainform.eightypercent.BackColor = Color.Red;
                mainform.ninetypercent.BackColor = Color.Red;
                mainform.hundredpercent.BackColor = Color.Red;
            }
            else 
            {
                if ((totalIncome - totalExpense) >= goal)
                {
                    mainform.tenpercent.BackColor = Color.Green;
                    mainform.twentypercent.BackColor = Color.Green;
                    mainform.thirtypercent.BackColor = Color.Green;
                    mainform.fortypercent.BackColor = Color.Green;
                    mainform.fiftypercent.BackColor = Color.Green;
                    mainform.sixtypercent.BackColor = Color.Green;
                    mainform.seventypercent.BackColor = Color.Green;
                    mainform.eightypercent.BackColor = Color.Green;
                    mainform.ninetypercent.BackColor = Color.Green;
                    mainform.hundredpercent.BackColor = Color.Green;
                }
                else if ((totalIncome - totalExpense) > goal * 0.9m)
                {
                    mainform.tenpercent.BackColor = Color.Green;
                    mainform.twentypercent.BackColor = Color.Green;
                    mainform.thirtypercent.BackColor = Color.Green;
                    mainform.fortypercent.BackColor = Color.Green;
                    mainform.fiftypercent.BackColor = Color.Green;
                    mainform.sixtypercent.BackColor = Color.Green;
                    mainform.seventypercent.BackColor = Color.Green;
                    mainform.eightypercent.BackColor = Color.Green;
                    mainform.ninetypercent.BackColor = Color.Green;
                    mainform.hundredpercent.BackColor = SystemColors.Control;
                }
                else if ((totalIncome - totalExpense) > goal * 0.8m)
                {
                    mainform.tenpercent.BackColor = Color.Green;
                    mainform.twentypercent.BackColor = Color.Green;
                    mainform.thirtypercent.BackColor = Color.Green;
                    mainform.fortypercent.BackColor = Color.Green;
                    mainform.fiftypercent.BackColor = Color.Green;
                    mainform.sixtypercent.BackColor = Color.Green;
                    mainform.seventypercent.BackColor = Color.Green;
                    mainform.eightypercent.BackColor = Color.Green;
                    mainform.ninetypercent.BackColor = SystemColors.Control;
                    mainform.hundredpercent.BackColor = SystemColors.Control;
                }
                else if ((totalIncome - totalExpense) > goal * 0.7m)
                {
                    mainform.tenpercent.BackColor = Color.Green;
                    mainform.twentypercent.BackColor = Color.Green;
                    mainform.thirtypercent.BackColor = Color.Green;
                    mainform.fortypercent.BackColor = Color.Green;
                    mainform.fiftypercent.BackColor = Color.Green;
                    mainform.sixtypercent.BackColor = Color.Green;
                    mainform.seventypercent.BackColor = Color.Green;
                    mainform.eightypercent.BackColor = SystemColors.Control;
                    mainform.ninetypercent.BackColor = SystemColors.Control;
                    mainform.hundredpercent.BackColor = SystemColors.Control;
                }
                else if ((totalIncome - totalExpense) > goal * 0.6m)
                {
                    mainform.tenpercent.BackColor = Color.Green;
                    mainform.twentypercent.BackColor = Color.Green;
                    mainform.thirtypercent.BackColor = Color.Green;
                    mainform.fortypercent.BackColor = Color.Green;
                    mainform.fiftypercent.BackColor = Color.Green;
                    mainform.sixtypercent.BackColor = Color.Green;
                    mainform.seventypercent.BackColor = SystemColors.Control;
                    mainform.eightypercent.BackColor = SystemColors.Control;
                    mainform.ninetypercent.BackColor = SystemColors.Control;
                    mainform.hundredpercent.BackColor = SystemColors.Control;
                }
                else if ((totalIncome - totalExpense) > goal * 0.5m)
                {
                    mainform.tenpercent.BackColor = Color.Green;
                    mainform.twentypercent.BackColor = Color.Green;
                    mainform.thirtypercent.BackColor = Color.Green;
                    mainform.fortypercent.BackColor = Color.Green;
                    mainform.fiftypercent.BackColor = Color.Green;
                    mainform.sixtypercent.BackColor = SystemColors.Control;
                    mainform.seventypercent.BackColor = SystemColors.Control;
                    mainform.eightypercent.BackColor = SystemColors.Control;
                    mainform.ninetypercent.BackColor = SystemColors.Control;
                    mainform.hundredpercent.BackColor = SystemColors.Control;
                }
                else if ((totalIncome - totalExpense) > goal * 0.4m)
                {
                    mainform.tenpercent.BackColor = Color.Green;
                    mainform.twentypercent.BackColor = Color.Green;
                    mainform.thirtypercent.BackColor = Color.Green;
                    mainform.fortypercent.BackColor = Color.Green;
                    mainform.fiftypercent.BackColor = SystemColors.Control;
                    mainform.sixtypercent.BackColor = SystemColors.Control;
                    mainform.seventypercent.BackColor = SystemColors.Control;
                    mainform.eightypercent.BackColor = SystemColors.Control;
                    mainform.ninetypercent.BackColor = SystemColors.Control;
                    mainform.hundredpercent.BackColor = SystemColors.Control;
                }
                else if ((totalIncome - totalExpense) > goal * 0.3m)
                {
                    mainform.tenpercent.BackColor = Color.Green;
                    mainform.twentypercent.BackColor = Color.Green;
                    mainform.thirtypercent.BackColor = Color.Green;
                    mainform.fortypercent.BackColor = SystemColors.Control;
                    mainform.fiftypercent.BackColor = SystemColors.Control;
                    mainform.sixtypercent.BackColor = SystemColors.Control;
                    mainform.seventypercent.BackColor = SystemColors.Control;
                    mainform.eightypercent.BackColor = SystemColors.Control;
                    mainform.ninetypercent.BackColor = SystemColors.Control;
                    mainform.hundredpercent.BackColor = SystemColors.Control;
                }
                else if ((totalIncome - totalExpense) > goal * 0.2m)
                {
                    mainform.tenpercent.BackColor = Color.Green;
                    mainform.twentypercent.BackColor = Color.Green;
                    mainform.thirtypercent.BackColor = SystemColors.Control;
                    mainform.fortypercent.BackColor = SystemColors.Control;
                    mainform.fiftypercent.BackColor = SystemColors.Control;
                    mainform.sixtypercent.BackColor = SystemColors.Control;
                    mainform.seventypercent.BackColor = SystemColors.Control;
                    mainform.eightypercent.BackColor = SystemColors.Control;
                    mainform.ninetypercent.BackColor = SystemColors.Control;
                    mainform.hundredpercent.BackColor = SystemColors.Control;
                }
                else if ((totalIncome - totalExpense) >= goal * 0.1m)
                {
                    mainform.tenpercent.BackColor = Color.Green;
                    mainform.twentypercent.BackColor = SystemColors.Control;
                    mainform.thirtypercent.BackColor = SystemColors.Control;
                    mainform.fortypercent.BackColor = SystemColors.Control;
                    mainform.fiftypercent.BackColor = SystemColors.Control;
                    mainform.sixtypercent.BackColor = SystemColors.Control;
                    mainform.seventypercent.BackColor = SystemColors.Control;
                    mainform.eightypercent.BackColor = SystemColors.Control;
                    mainform.ninetypercent.BackColor = SystemColors.Control;
                    mainform.hundredpercent.BackColor = SystemColors.Control;
                }

                else 
                {
                    mainform.tenpercent.BackColor = SystemColors.Control;
                    mainform.twentypercent.BackColor = SystemColors.Control;
                    mainform.thirtypercent.BackColor = SystemColors.Control;
                    mainform.fortypercent.BackColor = SystemColors.Control;
                    mainform.fiftypercent.BackColor = SystemColors.Control;
                    mainform.sixtypercent.BackColor = SystemColors.Control;
                    mainform.seventypercent.BackColor = SystemColors.Control;
                    mainform.eightypercent.BackColor = SystemColors.Control;
                    mainform.ninetypercent.BackColor = SystemColors.Control;
                    mainform.hundredpercent.BackColor = SystemColors.Control;
                }
            }

        }

    }
}
